/*
 * Copyright 2022-2029 heshuixing(https://gitee.com/heshuixing)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mercury.ec.framework.air.core.client.impl.qunar.util;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Map;

/**
 * 去哪儿接口工具类
 *
 * @author hesx
 */
@Slf4j
public class QunarUtils {

    /**
     * 获取参数签名
     *
     * @param params 请求参数
     * @param secret 签名密钥
     * @return 签名
     */
    public static String getQunarSign(Map<String, Object> params, String secret) {
        // 第一步：添加密钥
        params.put("key", secret);
        // 第二步：检查参数是否已经排序
        String[] keys = params.keySet().toArray(new String[0]);
        Arrays.sort(keys);
        // 第三步：把所有参数名和参数值串在一起
        StringBuilder query = new StringBuilder();
        for (String key : keys) {
            query.append(key).append("=").append(params.get(key));
        }
        params.remove("key");
        // 第四步：使用MD5加密
        return SecureUtil.md5(query.toString());
    }

    /**
     * 转化准点率
     *
     * @param correct
     * @return
     */
    public static BigDecimal getPunctualityRate(String correct) {
        if (StrUtil.isBlank(correct)) {
            return null;
        }
        correct = correct.replace("%", "").trim();
        if (StrUtil.isBlank(correct)) {
            return null;
        }
        return new BigDecimal(correct);
    }

    /**
     * 转化舱位等级
     *
     * @param cabinType 去哪儿舱位等级（0表示经济舱，1表示头等舱，2表示商务舱，3表示经济舱精选，4表示经济舱y舱，5表示超值头等舱）
     * @return
     */
    public static String getCabinType(Integer cabinType) {
        switch (cabinType) {
            case 0:
                return "经济舱";
            case 1:
                return "头等舱";
            case 2:
                return "商务舱";
            case 3:
                return "经济舱精选";
            case 4:
                return "经济舱y舱";
            case 5:
                return "超值头等舱";
        }
        return null;
    }

    /**
     * 获取航班天数
     *
     * @param departTime
     * @param arriveTime
     * @return
     */
    public static int getFlightDays(String departTime, String arriveTime) {
        int days = 0;
        if (StrUtil.isNotBlank(departTime) && StrUtil.isNotBlank(arriveTime)) {
            try {
                LocalTime departLocalTime = LocalTime.parse(departTime, DateTimeFormatter.ofPattern("HH:mm"));
                LocalTime arriveLocalTime = LocalTime.parse(arriveTime, DateTimeFormatter.ofPattern("HH:mm"));
                if (arriveLocalTime.isBefore(departLocalTime)) {
                    days = 1;
                }
            } catch (Exception e) {
                log.error("获取航班天数异常", e);
            }
        }
        return days;
    }
}
